/**
 * Copyright (c) 2019, NewStrength. All rights reserved.
 * 2019-01-08 16:07:07
 */
package cn.newstrength.lijingyu.step;

import cn.newstrength.wtdf.plugin.service.JWTService;
import cn.newstrength.wtdf.plugin.service.RedisService;
import cn.newstrength.wtdf.web.databus.DataContext;
import cn.newstrength.wtdf.web.exception.PAIException;
import cn.newstrength.wtdf.web.processor.Operation;
import cn.newstrength.wtdf.web.processor.OperationStep;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class LijyGetJWTTokenStep implements OperationStep {

	@Override
	public int excute(Operation oper) throws PAIException {
		DataContext ctx = oper.getContext();
		HttpServletRequest request = oper.getRequest();
		String accessToken = request.getHeader("accessToken");
		boolean verify = JWTService.verify(accessToken);
		if(!verify){
			ctx.setTranStepResult(-4,"Token 不合法",null);
			return 1;
		}
		RedisService redis = oper.getBean("redisService", RedisService.class);
		Object value = redis.get("cn:newstrength:nsms:oauth2:token:".concat(accessToken));
		if(value==null){
			ctx.setTranStepResult(-4,"Token 已过期",null);
		} else {
			ctx.setTranStepResult(value);
		}
		return 0;
	}

	@Override
	public int init(Map<String, String> input) {
		return 0;
	}

}
